package com.itboy.mall.pms.spuinfodesc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.spuinfodesc.bo.MallSpuInfoDescAddBo;
import com.itboy.mall.pms.spuinfodesc.bo.MallSpuInfoDescQueryBo;
import com.itboy.mall.pms.spuinfodesc.bo.MallSpuInfoDescEditBo;
import com.itboy.mall.pms.spuinfodesc.domain.MallSpuInfoDesc;
import com.itboy.mall.pms.spuinfodesc.mapper.MallSpuInfoDescMapper;
import com.itboy.mall.pms.spuinfodesc.vo.MallSpuInfoDescVo;
import com.itboy.mall.pms.spuinfodesc.service.IMallSpuInfoDescService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * spu信息介绍Service业务层处理
 *
 * @author itboy
 * @date 2021-08-08
 */
@Service
public class MallSpuInfoDescServiceImpl extends ServicePlusImpl<MallSpuInfoDescMapper, MallSpuInfoDesc> implements IMallSpuInfoDescService {

    @Override
    public MallSpuInfoDescVo queryById(Long spuId){
        return getVoById(spuId, MallSpuInfoDescVo.class);
    }

    @Override
    public TableDataInfo<MallSpuInfoDescVo> queryPageList(MallSpuInfoDescQueryBo bo) {
        PagePlus<MallSpuInfoDesc, MallSpuInfoDescVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSpuInfoDescVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSpuInfoDescVo> queryList(MallSpuInfoDescQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSpuInfoDescVo.class);
    }

    private LambdaQueryWrapper<MallSpuInfoDesc> buildQueryWrapper(MallSpuInfoDescQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSpuInfoDesc> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSpuId() != null, MallSpuInfoDesc::getSpuId, bo.getSpuId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSpuInfoDescAddBo bo) {
        MallSpuInfoDesc add = BeanUtil.toBean(bo, MallSpuInfoDesc.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSpuInfoDescEditBo bo) {
        MallSpuInfoDesc update = BeanUtil.toBean(bo, MallSpuInfoDesc.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSpuInfoDesc entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
